package com.lottery.common.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.text.SimpleDateFormat;
import java.util.Random;


public class AES {

    private static final String SKEY = "Baili@wlwang@123";
    public static final SimpleDateFormat MWFORMAT= new SimpleDateFormat("yyyyMMddHHmmss");


    private static final String IVSTR = "0201080306050704";

    /**
     * 加密
     * @param sSrc 需要加密的字符串
     * @return
     */
    public static String Encrypt(String sSrc ){
        try{
            if(sSrc==null || sSrc.length()<1){
                return null;
            }
            if( SKEY == null ) {

                return null;
            }
            if( SKEY.length() != 16 ) {

                return null;
            }
            byte[] raw = SKEY.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec( raw, "AES" );
            Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
            IvParameterSpec iv = new IvParameterSpec( IVSTR.getBytes() );
            cipher.init( Cipher.ENCRYPT_MODE, skeySpec, iv );
//            byte[] encrypted = cipher.doFinal( sSrc.getBytes() );
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));//加密
            return ObjectSerializer.encodeBytes( encrypted );
        }catch(Exception ex){
            return null;
        }

    }
    
    /**
     * 解密
     * @param sSrc 需要解密的字符串
     * @return
     */
    public static String Decrypt(String sSrc){
        try {
            // 判断Key是否正确
            if(sSrc==null || sSrc.length()<16){
                return null;
            }
            if( SKEY == null ) {
                System.out.print( "Key为空null" );
                return null;
            }
            // 判断Key是否为16位
            if( SKEY.length() != 16 ) {
                System.out.print( "Key长度不是16位" );
                return null;
            }
            byte[] raw = SKEY.getBytes( "ASCII" );
            SecretKeySpec skeySpec = new SecretKeySpec( raw, "AES" );
            Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
            IvParameterSpec iv = new IvParameterSpec( IVSTR.getBytes() );
            cipher.init( Cipher.DECRYPT_MODE, skeySpec, iv );

            byte[] encrypted1 = ObjectSerializer.decodeBytes( sSrc );
            try {
                byte[] original = cipher.doFinal( encrypted1 );
                String originalString = new String( original ,"utf-8");
                return originalString;
            }
            catch( Exception e ) {
                System.out.println( e.toString() );
                return null;
            }
        }
        catch(Exception ex ) {
            return null;
        }
    }

    /**
     * 解密
     * @param sSrc  需要解密的字符串
     * @param skey  KEY
     * @return
     */
    public static String Decrypt1(String sSrc, String skey){
        try {
            // 判断Key是否正确
            if(sSrc==null || sSrc.length()<16){
                return null;
            }
            if( skey == null ) {
                System.out.print( "Key为空null" );
                return null;
            }
            // 判断Key是否为16位
            if( skey.length() != 16 ) {
                System.out.print( "Key长度不是16位" );
                return null;
            }
            byte[] raw = skey.getBytes( "ASCII" );
            SecretKeySpec skeySpec = new SecretKeySpec( raw, "AES" );
            Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
            IvParameterSpec iv = new IvParameterSpec( IVSTR.getBytes() );
            cipher.init( Cipher.DECRYPT_MODE, skeySpec, iv );

            byte[] encrypted1 = ObjectSerializer.decodeBytes( sSrc );
            try {
                byte[] original = cipher.doFinal( encrypted1 );
                String originalString = new String( original );
                return originalString;
            }
            catch( Exception e ) {
                System.out.println( e.toString() );
                return null;
            }
        }
        catch(Exception ex ) {
            return null;
        }
    }

    /**
     * 
     * @param sSrc  需要加密的字符串
     * @param skey  KEY
     * @return
     */
    public static String Encrypt1(String sSrc, String skey){
        try{
//					if(sSrc==null || sSrc.length()<2){
//						return null;
//					}
            if( skey == null ) {
                System.out.print( "Key为空null" );
                return null;
            }
            // 判断Key是否为16位
            if( skey.length() != 16 ) {
                System.out.print( "Key长度不是16位" );
                return null;
            }
            byte[] raw = skey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec( raw, "AES" );
            Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );// "算法/模式/补码方式"
            IvParameterSpec iv = new IvParameterSpec( IVSTR.getBytes() );// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
            cipher.init( Cipher.ENCRYPT_MODE, skeySpec, iv );
            byte[] encrypted = cipher.doFinal( sSrc.getBytes() );

            return ObjectSerializer.encodeBytes( encrypted );
        }catch(Exception ex){
            return null;
        }

    }

    public static void main(String[] args) {
//        String str = "jiillpjeffbnfakfmpipdibplnimgefhppmjeajkbhoggnjgejmgkmkgfbnfpcjc";
    	System.out.println(Decrypt("ellmhaiohpnbfpgjnblappigadfbmdgm"));
        System.out.println(Encrypt("111111"));
//        System.out.println(Decrypt("hokgokijpknomdbgeihklpgdgdjnmcjf"));

//        for(int i=0;i<1000;i++){
//            System.out.println(String.valueOf(new Random().nextInt(899999) + 100000));
//        }
    }
}
